package mysql

import (
	"gorm.io/gorm"
)

// 商品分类表
type Category struct {
	gorm.Model
	ProductId int    `gorm:"type:int;comment:'商品id'"`
	Name      string `gorm:"type:varchar(50);not null;comment:'分类名称'"`
}

// 商品表
type Product struct {
	gorm.Model
	Name        string  `gorm:"type:varchar(50);not null;comment:'商品名称'"`
	CategoryID  uint    `gorm:"type:int;not null;comment:'分类id'"`
	Description string  `gorm:"type:varchar(100);not null;comment:'商品描述'"`
	Price       float64 `gorm:"type:decimal(10,2);not null;comment:'商品价格'"`
	Stock       int     `gorm:"type:int;not null;comment:'商品库存'"`
	Status      int8    `gorm:"default:1;comment:'0-待审核，1-已上架，2-已下架，3-审核拒绝'"`
}

// 商品图片表
type Image struct {
	gorm.Model
	ProductId int    `gorm:"type:int;comment:'商品id'"`
	Img       string `gorm:"type:varchar(500);not null;comment:'图片'"`
}

// 商品审核表
type Status struct {
	gorm.Model
	ProductID uint `gorm:"type:int;comment:'商品id'"`
	Status    int8 `gorm:"default:1;comment:'0-待审核，1-审核通过，2-审核拒绝'"`
}

func (g *Product) CreateProduct(DB *gorm.DB) error {
	tx := DB.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return err
	}

	if err := tx.Create(&g).Error; err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}
